package com.danciyixue.boot.dcyx.controller;

import com.danciyixue.boot.dcyx.service.UserStudyWordHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.danciyixue.boot.dcyx.model.form.UserStudyWordHistoryForm;
import com.danciyixue.boot.dcyx.model.query.UserStudyWordHistoryQuery;
import com.danciyixue.boot.dcyx.model.vo.UserStudyWordHistoryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 历史单词学习记录前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:20
 */
@Tag(name = "历史单词学习记录接口")
@RestController
@RequestMapping("/api/v1/user-study-word-history")
@RequiredArgsConstructor
public class UserStudyWordHistoryController  {

    private final UserStudyWordHistoryService userStudyWordHistoryService;

    @Operation(summary = "历史单词学习记录分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('dcyx:user-study-word-history:query')")
    public PageResult<UserStudyWordHistoryVO> getUserStudyWordHistoryPage(UserStudyWordHistoryQuery queryParams ) {
        IPage<UserStudyWordHistoryVO> result = userStudyWordHistoryService.getUserStudyWordHistoryPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增历史单词学习记录")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('dcyx:user-study-word-history:add')")
    public Result<Void> saveUserStudyWordHistory(@RequestBody @Valid UserStudyWordHistoryForm formData ) {
        boolean result = userStudyWordHistoryService.saveUserStudyWordHistory(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取历史单词学习记录表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('dcyx:user-study-word-history:edit')")
    public Result<UserStudyWordHistoryForm> getUserStudyWordHistoryForm(
        @Parameter(description = "历史单词学习记录ID") @PathVariable Long id
    ) {
        UserStudyWordHistoryForm formData = userStudyWordHistoryService.getUserStudyWordHistoryFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改历史单词学习记录")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('dcyx:user-study-word-history:edit')")
    public Result<Void> updateUserStudyWordHistory(
            @Parameter(description = "历史单词学习记录ID") @PathVariable Long id,
            @RequestBody @Validated UserStudyWordHistoryForm formData
    ) {
        boolean result = userStudyWordHistoryService.updateUserStudyWordHistory(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除历史单词学习记录")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('dcyx:user-study-word-history:delete')")
    public Result<Void> deleteUserStudyWordHistorys(
        @Parameter(description = "历史单词学习记录ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userStudyWordHistoryService.deleteUserStudyWordHistorys(ids);
        return Result.judge(result);
    }
}
